<template>
    <div class="partnerList">
        <div class="partnerList_top">
			<div class="into_index">首页<i class="el-icon-close"></i></div>
			<div class="current">新增分类<i class="el-icon-close"></i></div>
		</div>
        <div class="partnerList_bottom">
            <div class="partnerList_bottom_new">
				<div class="partnerList_bottom_new_goods">
					<div class="partnerList_bottom_new_goods_btn">
						<button @click="addLiveCate"><i class="el-icon-trophy"></i>新增分类</button>
					</div>
					<div class="partnerList_bottom_new_goods_screening">
						<div class="regionSelect">
							
						</div>
						<div class="partnerList_bottom_new_goods_screening_right">
							<el-input v-model="input" placeholder="请输入"></el-input>
							<button @click="handerSeach"><i class="el-icon-search"></i>搜索</button>
						</div>
					</div>
				</div>
			</div>
            <div class="partnerList_table">
                <div class="partnerList_bottom_table">
					<table>
						<tr>
							<th></th>
							<th>序号</th>
							<th>分类名称</th>
							<th>状态</th>
							<th>排序</th>
							<th>操作</th>
						</tr>
						<tr v-for="(v,i) in tableList" :key="i">
							<td>
								<font class="checkbox" :class="{'check_active' : checkBoxAll.includes(i)}" @click="handlerCheckbox(i)"><i class="el-icon-check"></i></font>
							</td>
							<td>{{v.id}}</td>
							<td>{{v.name}}</td>
							<td>{{v.status==0?'隐藏':v.status==1?'显示':''}}</td>
							<td>
								<el-input style="width:80px;margin-right:10px;" size="mini" v-model="v.sort"></el-input>
                                <el-button type="primary" style="width:60px;text-align:center;" size="mini" @click="changeSort(v.id,v.sort)">确定</el-button>
							</td>
							<td>
								<button class="editor" :class="{'shelves':v.status==1}" @click="toChangeStatus(v.id,v.status)">{{v.status==0?'上架':'下架'}}</button>
								<button class="delete" @click="toDel(v.id)">删除</button>
							</td>
						</tr>
						<tr v-if="tableList.length == 0"><td class="table_title" colspan="10">当前数据空空如也</td></tr>
					</table>
					<div class="partnerList_bottom_page">
						<div class="partnerList_bottom_page_left">
							<div class="partnerList_bottom_page_left_checkAll" @click="handerCheckAll">
								<font class="checkbox" :class="{'check_active' : checkAll}"><i class="el-icon-check"></i></font>
							</div>全选
							<!-- <button class="partnerList_bottom_page_left_btn" @click="selectDel">删除</button> -->
						</div>
						<div class="partnerList_bottom_page_right">
							<el-pagination
								@size-change="handleSizeChange"
								@current-change="handleCurrentChange"
								:current-page="page"
								:page-sizes="[10, 20, 30, 40]"
								:page-size="pageSize"
								layout="total, sizes, prev, pager, next, jumper"
								:total="total"
								background>
							</el-pagination>
						</div>
					</div>
				</div>
            </div>
        </div>
        <el-dialog
            title="新增分类"
            :visible.sync="dialogVisible"
            @closed="closedFn"
            width="40%">
            <div class="addBox">
                <div class="item"><span>分类名称</span><el-input v-model="cateName" style="width:50%;" placeholder="请输入分类名称" type="text" maxlength="10"></el-input></div>
                <div class="item"><span>分类排序</span><el-input v-model="cateSort" style="width:50%;" placeholder="请输入分类排序" type="number" ></el-input></div>
                <div class="item">
                    <span>是否上架</span>
                    <el-radio v-model="radioCate" label="1">是</el-radio>
                    <el-radio v-model="radioCate" label="0">否</el-radio>
                </div>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addBtn">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data(){
        return{
            cateList:[],
            cate:{},
             input: '',
			checkAll:false,
			page:1,
			pageSize:10,
			total:0,
            checkBoxAll:[],				// 选中的数据
			shelvesAlls:[],
            tableList:[],
            dialogVisible:false,
            radioCate:'0',
            cateName:'',
            cateSort:0
        }
    },
	mounted(){
		this.getEstateList()
	},

    methods:{
		// 获取直播分类列表
		getEstateList(){
			let params = {
				keyword:this.input,
				page:this.page,
				pagesize:this.pageSize
			}
			this.$http.post('/api/root/LiveCategory/index',params).then(res=>{
				console.log(res,'直播分类列表');
				if(res.code==1){
					this.tableList = res.data.list?res.data.list:[]
					this.total = res.data.total
				}
			})
		},
        // 更改状态
        toChangeStatus(id,sta){
            let params = {
                id,
                status:sta==0?1:0
            }
            this.$http.post('/api/root/LiveCategory/changeStatus',params).then(res=>{
                console.log(res,'状态更改结果');
                if(res.code==1){
                    this.$message.success('操作成功')
                    this.getEstateList()
                }else{
                    this.$message.warning(res.msg)
                }
            })
        },

		 // 排序按钮
        changeSort(id,sort){
            console.log(id,sort,'=====');
			let sor = {[`${id}`]:sort}
			let sortArr = JSON.stringify(sor)
            this.$http.post('/api/root/LiveCategory/saveOrder',{sort:sortArr}).then(res=>{
                console.log(res,'排序结果');
                if(res.code==1){
                    this.getEstateList()
                    this.$message.success('操作成功')
                }else{
                    this.$message.warning(res.msg)
                }
            })
        },
	
        // 删除按钮
        toDel(id){
            this.$confirm('确定删除该分类吗？','提示',{
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http.post('/api/root/LiveCategory/del',{id}).then(res=>{
                    console.log(res,'删除结果');
                    if(res.code==1){
                        this.getEstateList()
                        this.$message.success('删除成功')
                    }else{
                        this.$message.warning(res.msg)
                    }
                })
            }).catch(()=>{
                console.log('点击取消');
            })
        },
        
        // 搜索
        handerSeach(){
            this.getEstateList()
		},

		// 详情按钮
		toEdit(id){
			this.$router.push({name:'estateAdd',query:{id}})
		},

        // 状态选择
        statusSelectFn(e){
            console.log(e,'状态选择');
            this.getInfoList()
        },

        // 新增直播分类
        addLiveCate(){
            this.dialogVisible = true
        },
        // 新增分类对话框确定
        addBtn(){
            if(this.cateName==''){
                this.$message.warning('请输入分类名称')
                return 
            }
            let params = {
                name:this.cateName,
                status:this.radioCate,
                sort:this.cateSort
            }
            console.log(params,'添加分类数据');
            this.$http.post('/api/root/LiveCategory/add',params).then(res=>{
                console.log(res,'添加分类结果');
                if(res.code==1){
                    this.$message.success('添加成功')
                    this.getEstateList()
                    this.dialogVisible = false
                }else{
                    this.$message.warning(res.msg)
                }
            })
        },
        // 对h话框关闭后
        closedFn(){
            this.cateName = ''
            this.radioCate = '0'
        },
         
          // 多选
			handlerCheckbox(i){
                console.log(i);
				let arr = this.checkBoxAll;
				if(arr.includes(i)){
					let index = arr.indexOf(i);
					if(index != -1){
						arr.splice(index,1);
						this.shelvesAlls.splice(index,1);
						if(this.checkBoxAll.length !== this.tableList.length) {
							this.checkAll = false;
						}
					}
				}else {
					this.checkBoxAll.push(i);
					this.shelvesAlls.push(this.tableList[i].id);
					if(this.checkBoxAll.length == this.tableList.length) {
						this.checkAll = true;
					}
				};
				console.log(this.shelvesAlls);
			},
            // 全选与全不选
			handerCheckAll(){
				this.checkAll = !this.checkAll;
				let arr = this.tableList.length;
				this.checkBoxAll = [];
				this.shelvesAlls = [];
				if(this.checkAll){
					for(let i=0;i<arr;i++){
						this.checkBoxAll.push(i);
					}
					this.checkBoxAll.map(v => {
					this.shelvesAlls.push(this.tableList[v].id.toString());;
				})
				}else {
					this.checkBoxAll = [];
					this.shelvesAlls = []
				}
				console.log(this.shelvesAlls);
			},

			

        	handleSizeChange(val) {
				this.pageSize = val;
				this.getEstateList()
			},
			handleCurrentChange(val) {
			    this.page = val;
				this.getEstateList()
			},
    }
}
</script>

<style lang="less" scoped>
@import 'index.less';
</style>
